//
//  ShareAndSaveViewController.m
//  图片故事
//
//  Created by 大帅逼 on 16/3/8.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "ShareAndSaveViewController.h"
#import "Header.h"
#import "FWApplyFilter.h"
#import "MBProgressHUD+PKX.h"
#import "ShareCollectionViewCell.h"
//#import <UMSocial.h>
#import "CreateManagerViewController.h"

//UMSocialUIDelegate
@interface ShareAndSaveViewController ()

@property(nonatomic,strong) UIButton                *shareToStory;
@property(nonatomic,strong) UIButton                *saveBtn;
@property(nonatomic,strong) NSArray                 *iconArray;
@property(nonatomic,strong) UIView                  *bgView;
@property(nonatomic,strong) UIView                  *headView;
@property(nonatomic,strong) UIButton                *shareToSns;

@end

@implementation ShareAndSaveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavigation];
    
    [self initView];
}

- (void) initView {
    [self.view setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:1]];

    [self.view addSubview:self.bgView];
    
    [self.view addSubview:self.headView];
    
    [self.view addSubview:self.saveBtn];
    
    [self.view addSubview:self.shareToStory];
    
    [self.view addSubview:self.shareToSns];
}

- (void) initNavigation {
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:20]}];
    
    self.title = @"保存/分享";
}

#pragma mark -- 事件相关
- (void) action_backBtnClickEvent {

    [self.navigationController popViewControllerAnimated:YES];
}

- (void) action_shareTiStoryBtnClick {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"addToImageStory" object:_resultImage];
    
    [self.navigationController popViewControllerAnimated:YES];
    
    [[CreateManagerViewController shardManger] showImageProcessViewControllerWithImage:_resultImage];
}

- (void) action_shareToSnsBtnClick {

    /*
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"56dea03167e58efba8001091"
                                      shareText:@"通过imgs处理的美图"
                                     shareImage:_resultImage
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToDouban,nil]
                                       delegate:self];
     */
}

- (void)saveImageToPhotos
{

    UIImageWriteToSavedPhotosAlbum(_resultImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}

// 保存图片事件回调
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
    if(error == NULL){

        [MBProgressHUD showSuccess:@"保存成功"];
    }else {
    
        [MBProgressHUD showError:@"保存失败"];
    }
}


#pragma mark -- 懒加载

- (UIButton *)saveBtn {
    
    if (!_saveBtn) {
        
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_saveBtn setBounds:CGRectMake(0, 0, My_Width(200), My_Height(40))];
        
        [_saveBtn setCenter:CGPointMake(SCREEN_W / 2, My_Height(200))];
        
        [_saveBtn.layer setCornerRadius:My_Height(20.0)];
        
        [_saveBtn setTitle:@"保存到相册" forState:UIControlStateNormal];
        
        [_saveBtn setBackgroundColor:[UIColor colorWithRed:176/255.0 green:196/255.0 blue:222/255.0 alpha:0.7]];
        
        [_saveBtn addTarget:self action:@selector(saveImageToPhotos) forControlEvents:UIControlEventTouchUpInside];
        
        [_saveBtn.layer setBorderWidth:1];
        
        [_saveBtn.titleLabel setTextColor:[UIColor whiteColor]];
        
        [_saveBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        
        [_saveBtn.titleLabel setTextAlignment:NSTextAlignmentCenter];
        
        [_saveBtn.layer setBorderColor:[UIColor whiteColor].CGColor];
    }
    
    return _saveBtn;
}

- (UIButton *)shareToStory {

    if (!_shareToStory) {
        
        _shareToStory = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_shareToStory setBounds:CGRectMake(0, 0, My_Width(200), My_Height(40))];
        
        [_shareToStory setCenter:CGPointMake(SCREEN_W / 2, My_Height(290))];
        
        [_shareToStory.layer setCornerRadius:My_Height(20.0)];
        
        [_shareToStory setTitle:@"添加到图片故事" forState:UIControlStateNormal];
        
        [_shareToStory setBackgroundColor:[UIColor colorWithRed:176/255.0 green:196/255.0 blue:222/255.0 alpha:0.7]];
        
        [_shareToStory addTarget:self action:@selector(action_shareTiStoryBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        [_shareToStory.layer setBorderWidth:1];
        
        [_shareToStory.titleLabel setTextColor:[UIColor whiteColor]];
        
        [_shareToStory.titleLabel setFont:[UIFont systemFontOfSize:16]];
        
        [_shareToStory.titleLabel setTextAlignment:NSTextAlignmentCenter];
        
        [_shareToStory.layer setBorderColor:[UIColor whiteColor].CGColor];
    }
    
    return _shareToStory;
}

- (UIButton *)shareToSns {
    
    if (!_shareToSns) {
        
        _shareToSns = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_shareToSns setBounds:CGRectMake(0, 0, My_Width(200), My_Height(40))];
        
        [_shareToSns setCenter:CGPointMake(SCREEN_W / 2, My_Height(380))];
        
        [_shareToSns.layer setCornerRadius:My_Height(20.0)];
        
        [_shareToSns setTitle:@"分享到社交软件" forState:UIControlStateNormal];
        
        [_shareToSns addTarget:self action:@selector(action_shareToSnsBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        [_shareToSns setBackgroundColor:[UIColor colorWithRed:176/255.0 green:196/255.0 blue:222/255.0 alpha:0.7]];
        
        [_shareToSns.layer setBorderWidth:1];
        
        [_shareToSns.titleLabel setTextColor:[UIColor whiteColor]];
        
        [_shareToSns.titleLabel setFont:[UIFont systemFontOfSize:16]];
        
        [_shareToSns.titleLabel setTextAlignment:NSTextAlignmentCenter];
        
        [_shareToSns.layer setBorderColor:[UIColor whiteColor].CGColor];
    }
    
    return _shareToSns;
}


- (UIView *)bgView {

    if (!_bgView) {
        
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
        
        UIImageView *bgImageView= [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
        
        [bgImageView setImage:_resultImage];
        
        [_bgView addSubview:bgImageView];
        
        UIView *maskview =[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
        
        [maskview setBackgroundColor:[UIColor blackColor]];
        
        [maskview setAlpha:0.7];
        
        [_bgView addSubview:maskview];
    }
    
    return _bgView;
}

- (UIView *)headView {

    if (!_headView) {
        
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_W, 44)];
        
        UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 7, 30, 30)];
        
        [backBtn setImage:[UIImage imageNamed:@"back_icon"] forState:UIControlStateNormal];
        
        [backBtn addTarget:self action:@selector(action_backBtnClickEvent) forControlEvents:UIControlEventTouchUpInside];
        
        [_headView addSubview:backBtn];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        
        [titleLabel setBounds:CGRectMake(0, 0, My_Width(100), 20)];
        
        [titleLabel setCenter:CGPointMake(SCREEN_W / 2, 22)];
        
        [titleLabel setTextAlignment:NSTextAlignmentCenter];
        
        [titleLabel setText:@"保存／分享"];
        
        [titleLabel setTextColor:[UIColor whiteColor]];
        
        [_headView addSubview:titleLabel];
    }
    
    return _headView;
}

- (void)viewWillAppear:(BOOL)animated {

    [self.navigationController setNavigationBarHidden:YES];
}

- (void) viewWillDisappear:(BOOL)animated {

    [self.navigationController setNavigationBarHidden:NO];
}

@end
